package com.example.util;

import com.example.dao.AdminDAO;
import com.example.model.TradeInfo;
import com.example.model.UserInfo;

import javax.swing.table.AbstractTableModel;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

public class TradeInfoTableModel extends AbstractTableModel {
    AdminDAO adminDAO = new AdminDAO();
    private List<TradeInfo> tradeInfos = adminDAO.getTradeInfo();
    private final String[] columnNames = {"交易ID", "日期", "类型", "金额", "卡号", "备注"};



    public TradeInfoTableModel(List<TradeInfo> tradeInfos) {
        this.tradeInfos = tradeInfos;
    }

    @Override
    public int getRowCount() {
        return tradeInfos.size();
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        TradeInfo tradeInfo = tradeInfos.get(rowIndex);
        return switch (columnIndex) {
            case 0 -> tradeInfo.getTradeId();
            case 1 -> tradeInfo.getTradeDate();
            case 2 -> tradeInfo.getTradeType();
            case 3 -> tradeInfo.getTradeMoney();
            case 4 -> tradeInfo.getCardId();
            case 5 -> tradeInfo.getRemark();
            default -> null;
        };
    }

    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        // 只有ID列不可编辑
        return false;
    }

//    @Override
//    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
//        TradeInfo tradeInfo = tradeInfos.get(rowIndex);
//        switch (columnIndex) {
//            case 1 -> tradeInfo.setTradeDate(LocalDateTime.parse((String) aValue));
//            case 2 -> tradeInfo.setTradeType((String) aValue);
//            case 3 -> tradeInfo.setTradeMoney((BigDecimal) aValue);
//            case 4 -> tradeInfo.setCardId((String) aValue);
//            case 5 -> tradeInfo.setRemark((String) aValue);
//        }
//        adminDAO.updateTradeInfo(tradeInfo);
//        fireTableCellUpdated(rowIndex, columnIndex);
//    }
}